# http调试脚本
import requests
import json

# 基础URL
BASE_URL = "http://localhost:3000"

# 存储认证token
token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjEsInVzZXJuYW1lIjoidGVzdHVzZXIiLCJpYXQiOjE3NTY0NTAyNTgsImV4cCI6MTc1NjUzNjY1OH0.CeyJUDFXuPwViFmipJv3R--rUZM3veUq4PCGFuLwo_Y'


def print_response(response):
    """打印响应结果"""
    print(f"Status Code: {response.status_code}")
    try:
        print(f"Response: {json.dumps(response.json(), indent=2)}")
    except:
        print(f"Response: {response.text}")
    print("-" * 50)


# 1. 用户注册
def register_user():
    print("1. 用户注册")
    url = f"{BASE_URL}/register"
    payload = {
        "email": "mario5@example.com",
        "username": "mario5",
        "fullName":"秦国宝",
        "password": "password123",
        "firstName": "Test",
        "lastName": "User"
    }
    headers = {
        "Content-Type": "application/json"
    }
    response = requests.post(url, json=payload, headers=headers)
    print_response(response)
    return response


# 2. 用户登录
def login_user():
    global token
    print("2. 用户登录")
    url = f"{BASE_URL}/login"
    payload = {
        "username": "mario5",
        "password": "password123"
    }
    headers = {
        "Content-Type": "application/json"
    }
    response = requests.post(url, json=payload, headers=headers)
    print_response(response)

    # 保存token用于后续请求
    if response.status_code == 200:
        token = response.json().get("token")
        print(f"Token saved: {token}")
    return response
# eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjEsInVzZXJuYW1lIjoidGVzdHVzZXIiLCJpYXQiOjE3NTY0NTAyNTgsImV4cCI6MTc1NjUzNjY1OH0.CeyJUDFXuPwViFmipJv3R--rUZM3veUq4PCGFuLwo_Y


# 3. 修改密码
def change_password():
    print("3. 修改密码")
    # 修改URL路径和方法
    url = f"{BASE_URL}/users/1/password"  # 假设用户ID为1
    payload = {
        "oldPassword": "password123",    # 参数名从currentPassword改为oldPassword
        "newPassword": "newpassword123"
    }
    headers = {
        "Authorization": f"Bearer {token}",
        "Content-Type": "application/json"
    }
    # 修改请求方法为PUT
    response = requests.put(url, json=payload, headers=headers)
    print_response(response)
    return response


# 4. 获取用户列表 (管理员)
def get_users_list():
    print("4. 获取用户列表 (管理员)")
    url = f"{BASE_URL}/users"
    headers = {
        "Authorization": f"Bearer {token}"
    }
    response = requests.get(url, headers=headers)
    print_response(response)
    return response


# 5. 获取个人信息
def get_user_profile():
    print("5. 获取个人信息")
    url = f"{BASE_URL}/users/profile"
    headers = {
        "Authorization": f"Bearer {token}"
    }
    response = requests.get(url, headers=headers)
    print_response(response)
    return response


# 6. 获取指定用户 (管理员)
def get_user_by_id(user_id):
    print(f"6. 获取指定用户 (管理员): {user_id}")
    url = f"{BASE_URL}/users/{user_id}"
    headers = {
        "Authorization": f"Bearer {token}"
    }
    response = requests.get(url, headers=headers)
    print_response(response)
    return response


# 7. 更新个人信息
def update_profile():
    print("7. 更新个人信息")
    url = f"{BASE_URL}/users/1"
    payload = {
        "username": "Updatedusername",
        "email": "updatedemail"
    }
    headers = {
        "Authorization": f"Bearer {token}",
        "Content-Type": "application/json"
    }
    response = requests.patch(url, json=payload, headers=headers)
    print_response(response)
    return response


# 8. 更新用户信息 (管理员)
def update_user_by_id(user_id):
    print(f"8. 更新用户信息 (管理员): {user_id}")
    url = f"{BASE_URL}/users/{user_id}"
    payload = {
        "username": "Updatedusername",
        "email": "updatedemail"
    }
    headers = {
        "Authorization": f"Bearer {token}",
        "Content-Type": "application/json"
    }
    response = requests.put(url, json=payload, headers=headers)
    print_response(response)
    return response


# 9. 删除用户 (管理员)
def delete_user_by_id(user_id):
    print(f"9. 删除用户 (管理员): {user_id}")
    url = f"{BASE_URL}/users/{user_id}"
    headers = {
        "Authorization": f"Bearer {token}"
    }
    response = requests.delete(url, headers=headers)
    print_response(response)
    return response


# 10. 激活用户 (管理员)
def activate_user(user_id):
    print(f"10. 激活用户 (管理员): {user_id}")
    url = f"{BASE_URL}/users/{user_id}/activate"
    headers = {
        "Authorization": f"Bearer {token}"
    }
    response = requests.patch(url, headers=headers)
    print_response(response)
    return response


# 11. 停用用户 (管理员)
def deactivate_user(user_id):
    print(f"11. 停用用户 (管理员): {user_id}")
    url = f"{BASE_URL}/users/{user_id}/deactivate"
    headers = {
        "Authorization": f"Bearer {token}"
    }
    response = requests.patch(url, headers=headers)
    print_response(response)
    return response


# 执行测试流程
if __name__ == "__main__":
    print("开始HTTP接口调试\n")

    # 用户注册
    # register_user()

    # # 用户登录
    login_user()
    #
    # # 修改密码
    # change_password()
    #
    # # 获取用户列表
    # get_users_list()
    #
    # # 获取个人信息
    # get_user_profile()
    #
    # # 其他需要用户ID的操作，这里假设用户ID为1
    # user_id = 1
    #
    # # 获取指定用户
    # get_user_by_id(user_id)
    #
    # # 更新个人信息
    # update_profile()
    #
    # # 更新用户信息
    # update_user_by_id(1)
    #
    # # 激活用户
    # activate_user(user_id)
    #
    # # 停用用户
    # deactivate_user(user_id)

    # 删除用户
    # delete_user_by_id(user_id)  # 注意：删除操作请谨慎测试
